package com.nfit.shopweb.web.api;

import com.github.pagehelper.PageInfo;
import com.nfit.shopmodel.dto.system.AssginMenuDto;
import com.nfit.shopmodel.dto.system.AssginRoleDto;
import com.nfit.shopmodel.dto.system.SysRoleDto;
import com.nfit.shopmodel.entity.system.SysRole;
import com.nfit.shopmodel.entity.system.SysUser;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import com.nfit.shopweb.service.SysRoleService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName:SysRoleController
 * Package:com.nfit.shopweb.web.api
 * Description
 *
 * @Author L
 * @Create 2024/11/17 13:00
 */
@Tag(name = "role",description = "角色管理")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    private static final Logger log = LoggerFactory.getLogger(SysRoleController.class);
    @Autowired
    private SysRoleService sysRoleService;


    @PostMapping("/page/{pageNum}/{pageSize}")
    public Result page(@RequestBody(required = false) SysRoleDto sysRoleDto,
                       @PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize) {
        PageInfo<SysRole> list = sysRoleService.page(pageNum, pageSize, sysRoleDto);
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        sysRoleService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        sysRoleService.update(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据用户id查询自己分配到的角色数据
     */
    @GetMapping("/findAll/{userId}")
    public Result findAll(@PathVariable("userId") Long userId) {
       Map<String,Object> map = sysRoleService.findAll(userId);
       return Result.build(map, ResultCodeEnum.SUCCESS);
    }

    /**
     * 分配菜单
     * 根据角色id查询角色对应的菜单id和菜单数据,封装在map中
     */
    @GetMapping("/findByRoleIdAssignMenu/{roleId}")
    public Result<Map<String,Object>> findByRoleIdAssignMenu(@PathVariable("roleId") Long roleId) {
        Map<String,Object> map = sysRoleService.findByRoleIdAssignMenu(roleId);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存分配的id
     * @param assginMenuDto 封装前端传递的菜单id
     */
    @PostMapping("/saveAssignMenu")
    public Result saveAssignMenu(@RequestBody AssginMenuDto assginMenuDto) {
        log.info("角色id:{}",assginMenuDto.getRoleId());
        log.info("菜单id集合:{}",assginMenuDto.getMenuIdList());
        sysRoleService.saveAssignMenu(assginMenuDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
